<template>
  <el-card class="attr-card">
    <!-- @submit.prevent 阻止默认提交行为 -->
    <el-form @submit.prevent>
      <el-form-item label="属性名">
        <el-input
          ref="input"
          placeholder="请输入属性名"
          class="attr-input"
          v-model="attrFormData.attrName"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <!--  :disabled="!attrFormData.attrName" 没有值就禁用 -->
        <el-button
          type="primary"
          :icon="Plus"
          :disabled="!attrFormData.attrName"
          @click="showAddAttrValue"
          >添加属性值</el-button
        >

        <!-- 这块的问题 刚才写的是这个:data="[{}]" -->
        <el-table :data="attrFormData.attrValueList" border class="attr-table">
          <el-table-column
            type="index"
            label="序号"
            width="60"
            align="center"
          ></el-table-column>

          <el-table-column label="属性值名称">
            <template v-slot="{ row, $index }">
              <el-input
                ref="input"
                size="small"
                v-if="row.isEdit"
                v-model="row.valueName"
                @blur="goShow(row, $index)"
                @keyup.enter="goShow(row, $index)"
              />
              <div v-else @click="goEdit(row)">{{ row.valueName }}</div>
            </template>
          </el-table-column>

          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-tooltip content="删除属性值" placement="top" effect="light">
                <div class="attr-wrap">
                  <el-popconfirm
                    :title="`您确定要删除${row.valueName}属性值吗`"
                    @confirm="delAttrValue($index)"
                  >
                    <template #reference>
                      <el-button type="danger" :icon="Delete" size="small" />
                    </template>
                  </el-popconfirm>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :disabled="isBtnDisabled" @click="submitForm">
          确定
        </el-button>
        <el-button @click="emit('setIsShowAttrList', true)">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SaveOrUpdateAttr",
};
</script>

<script lang="ts" setup>
import { Plus, Delete } from "@element-plus/icons-vue";
import { reactive, ref, nextTick, computed } from "vue";
import type {
  SaveAttrItem,
  SaveAttrValueItem,
} from "@/api/product/model/attrModel";
import { saveAttrApi } from "@/api/product/attr";
import { ElMessage } from "element-plus";
import { useCategoryStore } from "@/stores/category";
import { onMounted, onBeforeMount } from "vue";

const categoryStore = useCategoryStore();

const emit = defineEmits(["setIsShowAttrList"]);

const props = defineProps<{
  attr: SaveAttrItem;
}>();

// 获取el-input
const input = ref();

// 收齐表单一组数据
const attrFormData = reactive<SaveAttrItem>(props.attr);

// 点击添加值
const showAddAttrValue = async () => {
  attrFormData.attrValueList.push({
    valueName: "",
    isEdit: true, // 每一行数据的标识，代表是否显示编辑模式
  });
  // 获取dom
  await nextTick();

  // 获取焦点
  input.value.focus();
};

// 切换焦点显示模式
const goShow = (row: SaveAttrValueItem, index: number) => {
  if (!row.valueName) {
    // 无值
    attrFormData.attrValueList.splice(index, 1);
    return;
  }
  // 有值
  row.isEdit = false;
};

// 点击切换编辑模式
const goEdit = async (row: SaveAttrValueItem) => {
  // 变成编辑模式
  row.isEdit = true;
  // dom
  await nextTick();
  // 聚焦
  input.value.focus();
};

// 点击删除
const delAttrValue = (index: number) => {
  attrFormData.attrValueList.splice(index, 1);
};

// 监视控制按钮禁用效果
const isBtnDisabled = computed(() => {
  /*
     1. 属性名需要有值
     2. 属性值至少有一个值
     3. 属性值至少有一个元素有valueName
  */
  return (
    !attrFormData.attrName ||
    !attrFormData.attrValueList.find((attrValue) => attrValue.valueName)
  );
});

// 添加
const submitForm = async () => {
  // 请求
  await saveAttrApi({
    ...attrFormData,
    categoryId: categoryStore.category3Id as number,
    categoryLevel: 3, //三级分类
  });
  ElMessage.success(`${attrFormData.id ? "更新" : "添加"}`);
  emit("setIsShowAttrList", true);
};

onBeforeMount(() => {
  categoryStore.category1Id = undefined;
  categoryStore.category2Id = undefined;
  categoryStore.category3Id = undefined;
  categoryStore.category1List = [];
  categoryStore.category2List = [];
  categoryStore.category3List = [];
});
</script>

<style scoped>
.attr-card {
  margin-top: 20px;
}
.attr-input {
  width: 200px;
}
.attr-table {
  margin-top: 10px;
}
.attr-wrap {
  display: inline-block;
}
</style>
